package ua.kiev.tosik.price.modules.clients.tools;

import org.apache.commons.lang.StringUtils;
import ua.kiev.tosik.price.models.Prices;

public class ValidatorPrice implements IPriceValidator {

	private final String[] whatReplace = {",", "$", " "};
	private final String[] toReplace = {".", "", ""};

	@Override
	public boolean isValid(Object value) {
		try {
			// Double.valueOf(value.toString().replaceAll(",", ".").replaceAll("\\$", ""));
			Double.valueOf(StringUtils.replaceEach(value.toString().replaceAll(" ", ""), whatReplace, toReplace));
			return true;
		} catch (Exception e) {
			return false;
		}
	}

	@Override
	public Prices fillData(Prices prices, Object value) {
		double priceValue = Double.valueOf(StringUtils.replaceEach(value.toString().replaceAll(" ", ""), whatReplace, toReplace));
		prices.setPrice(priceValue);
		return prices;
	}

}
